//
//  AskViewController.m
//  Qyer
//
//  Created by dllo on 15/10/24.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "AskViewController.h"

@interface AskViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, assign)CGFloat cellHeight;
@property (nonatomic, assign)BOOL isLoad;
@end

@implementation AskViewController

- (void)viewWillDisappear:(BOOL)animated {
    [GiFHUD dismiss];
}
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self creatCustomNavBar];
    self.page = 1;
    [self creatAskTableView];
    _dataArr = [[NSMutableArray alloc] init];
    self.isLoad = NO;
    [self getData];
    [GiFHUD show];
    
}
- (void)creatCustomNavBar {
    UIView *navBar = [[UIView alloc] initWithFrame:CRM(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 0.4)];
    navBar.backgroundColor = NAVBAR_COLOR;
    [self.view addSubview:navBar];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CRM(20, 30, 25, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"fanhui-white"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CRM(55, 22, 50, 35)];
    label.font = [UIFont systemFontOfSize:18.];
    label.text = @"问答";
    label.textColor = WHITE_COLOR;
    [self.view addSubview:label];
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CRM(0, 0, 150, 50);
    [searchButton setTitle:@"点击搜索问题" forState:UIControlStateNormal];
    [navBar addSubview:searchButton];
    searchButton.center = CGPointMake(SCREEN_WIDTH / 2, navBar.center.y + 20);
    [searchButton addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)searchClick {
    SeachAskVc *askVc = [[SeachAskVc alloc] init];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController pushViewController:askVc animated:YES];
}
- (void)backClick:(UIButton *)sender {
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)creatAskTableView {
    
    _askTableView = [[UITableView alloc] initWithFrame:CRM(0, SCREEN_WIDTH * 0.4, SCREEN_WIDTH, SCREEN_HEIGHT - SCREEN_WIDTH * 0.4) style:UITableViewStylePlain];
    _askTableView.dataSource = self;
    _askTableView.delegate = self;
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    _askTableView.tableFooterView = view;
    [self.view insertSubview:_askTableView atIndex:0];
    
}
- (void)getData {
    [GiFHUD show];
    if ([Reachability isConnectedInternet]) {
        //NSLog(@"self.page == %ld", self.page);
        [AFN getdataWithString:[NSString stringWithFormat:@"http://open.qyer.com/qyer/ask/question/get_new?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88274251598133&lon=121.5397798076504&oauth_token=aa3e2c9cfa1fa80cc75956eeb20534b3&page=%ld", self.page] block:^(id block) {
//            if (_isLoad == NO) {
//                [_dataArr removeAllObjects];
//            }
            for (NSDictionary *dic in [block objectForKey:@"data"]) {
                AskModel *model = [AskModel modelWithDictionary:dic];
                [_dataArr addObject:model];
            }
//            if (_askTableView.legendFooter == nil) {
//                // 避免强引用
//                __weak AskViewController *askVc = self;
//                [_askTableView addLegendFooterWithRefreshingBlock:^{
//                    _isLoad = YES;
//                    askVc.page = askVc.page + 1;
//                    NSLog(@"%ld", askVc.page);
//                    [askVc getData];
//                }];
//            }
            //[_askTableView.legendFooter endRefreshing];
            [_askTableView reloadData];
            [GiFHUD dismiss];
        } BLOCK:^(id BLOCK) {
            [GiFHUD dismiss];
        }];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    AskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[AskTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    AskModel *model = [_dataArr objectAtIndex:indexPath.row];
    [cell setCellWithModel:model text:@""];
    self.cellHeight = cell.height;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //AskTableViewCell *cell = (AskTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    return self.cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AskModel *model = [_dataArr objectAtIndex:indexPath.row];
    CommunityWeb *web = [[CommunityWeb alloc] init];
    web.urlStr = model.appview_url;
    [self.navigationController pushViewController:web animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
